<?php
/*********************************************
  CPG Dragonfly™ CMS
  ********************************************
  Copyright © 2004 - 2007 by CPG-Nuke Dev Team
  http://dragonflycms.org

  Dragonfly is released under the terms and conditions
  of the GNU GPL version 2 or any later version

  Setup the first administrator account
**********************************************/
if (!defined('INSTALL')) { exit; }

# Pick a timezone
$tz_select = '<select name="timezone" class="formfield">';
foreach (timezone_identifiers_list() as $tz) {
	$sel = ('UTC' == $tz) ? ' selected="selected"' : '';
	$tz_select .= '<option value="'.$tz.'"'.$sel.'>'.$tz.'</option>';
}
$tz_select .= '</select>';

if (!$db->count('admins')) {
	if (!isset($_POST['pwd'])) {
		inst_header();
		echo '<script language="JavaScript" type="text/javascript">
<!--'."
maketip('nickname','"._NICKNAME."','".$instlang['s3_nick2']."');
maketip('email','"._EMAIL."','".$instlang['s3_email2']."');
maketip('password','"._PASSWORD."','".$instlang['s3_pass2']."');
maketip('timezone','".$instlang['s3_timezone']."','".$instlang['s3_timezone2']."');
".'// -->
</script>
'._NOADMINYET.'<br />'.$instlang['s3_warning'].'<br /><br />'
		.'<table border="0">'
		.'<tr><td>'._NICKNAME.'</td><td><input type="text" name="name" size="30" maxlength="25" class="formfield" /> '.inst_help('nickname').'</td></tr>'
		.'<tr><td>'._EMAIL.'</td><td><input type="text" name="email" size="30" maxlength="255" class="formfield" /> '.inst_help('email').'</td></tr>'
		.'<tr><td>'._PASSWORD.'</td><td><input type="password" name="pwd" size="20" class="formfield" /> '.inst_help('password').'</td></tr>'
		.'<tr><td>'.$instlang['s3_timezone'].'</td><td>'.$tz_select.' '.inst_help('timezone').'</td></tr>'
		.'<tr><td colspan="2">'._CREATEUSERDATA.' <input type="radio" name="user_new" value="1" checked="checked" />'._YES.'&nbsp;&nbsp;<input type="radio" name="user_new" value="0" />'._NO.'<br /><br />'
		.'<input type="hidden" name="step" value="5" /><input type="submit" value="'._SUBMIT.'" class="formfield" />'
		.'</td></tr></table>';
	} else if (preg_match('#[0-9]#', $_POST['pwd']) && preg_match('#[a-z]#', $_POST['pwd']) && preg_match('#[A-Z]#', $_POST['pwd'])) {
		$cookie = unserialize(base64_decode($_COOKIE['installtest']));
		$name  = $_POST['name'];
		$email = $_POST['email'];
		$db->TBL->admins->insert(array(
			'admin_id' => 1,
			'aid'   => $name,
			'email' => $email,
			'pwd'   => \Poodle\Auth::hashPassword($_POST['pwd']),
			'radminsuper' => 1
		));
		if (!empty($_POST['user_new'])) {
			$user_id = $db->TBL->users->insert(array(
				'username'         => $_POST['name'],
				'user_nickname_lc' => mb_strtolower($_POST['name']),
				'user_email'       => $_POST['email'],
				'user_avatar'      => 'gallery/blank.gif',
				'user_regdate'     => time(),
				'theme'            => '',
				'user_level'       => 2,
				'user_timezone'    => $_POST['timezone'],
			), 'user_id');
			\Poodle\Identity\Search::byID($user_id)->updateAuth(1, $_POST['name'], $_POST['pwd']);
			Dragonfly_Identity_Cookie::set($user_id);
		}
		Dragonfly_Admin_Cookie::set(1);
		setcookie('installtest','',-1,trim($cookie['cookiepath']),trim($cookie['cookiedom'])); //, int secure
		$images[3] = 'checked';
		inst_header();
		echo $instlang['s3_finnish'];
	} else {
		inst_header();
		echo '<script language="JavaScript" type="text/javascript">
<!--'."
maketip('timezone','".$instlang['s3_timezone']."','".$instlang['s3_timezone2']."');
".'// -->
</script>
<b>'._ERROR.': '.$instlang['s3_warning'].'</b><br /><br />'
		."<table border=\"0\">"
		."<tr><td>"._NICKNAME."</td><td><input type=\"text\" name=\"name\" size=\"30\" maxlength=\"25\" value=\"$name\" class=\"formfield\" /></td></tr>"
		."<tr><td>"._EMAIL."</td><td><input type=\"text\" name=\"email\" size=\"30\" maxlength=\"255\" value=\"$email\" class=\"formfield\" /></td></tr>"
		."<tr><td>"._PASSWORD."</td><td><input type=\"password\" name=\"pwd\" size=\"20\" class=\"formfield\" /></td></tr>"
		.'<tr><td>Timezone</td><td>'.$tz_select.'</td></tr>'
		.'<tr><td colspan="2">'._CREATEUSERDATA.'  <input type="radio" name="user_new" value="1" checked="checked" />'._YES.'&nbsp;&nbsp;<input type="radio" name="user_new" value="0" />'._NO.'<br /><br />'
		.'<input type="hidden" name="step" value="5" /><input type="submit" value="'._SUBMIT.'" class="formfield" />'
		.'</td></tr></table>';
  }
}
